package cn.windy.samary.module.admin.service.impl;

import cn.windy.samary.module.abstracts.service.impl.AbstractServiceImpl;
import cn.windy.samary.module.admin.common.UserLine;
import cn.windy.samary.module.admin.entity.Notify;
import cn.windy.samary.module.admin.entity.NotifyRecord;
import cn.windy.samary.module.admin.mapper.NotifyMapper;
import cn.windy.samary.module.admin.mapper.NotifyRecordMapper;
import cn.windy.samary.module.admin.service.INotifyRecordService;
import cn.windy.samary.module.admin.service.INotifyService;
import cn.windy.samary.module.admin.service.ISessionService;
import cn.windy.samary.utils.ShiroUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zyl
 * @date 2018-06-05 15:55
 */
@Service
public class NotifyServiceImpl extends AbstractServiceImpl<Notify> implements INotifyService {

    @Resource
    private INotifyRecordService notifyRecordService;
    @Resource
    private ISessionService sessionService;
    @Resource
    private ThreadPoolTaskExecutor executor;
    @Autowired
    private SimpMessagingTemplate template;
    @Resource
    private NotifyRecordMapper notifyRecordMapper;
    @Resource
    private Sid sid;


    @Override
    public int insertSelective(Notify notify) {
        int i = super.insertSelective(notify);
        String[] userIds = notify.getUserIds();
        if (userIds != null && userIds.length != 0) {
            List<NotifyRecord> notifyRecords = new ArrayList<>();
            String notifyId = notify.getId();
            Date date = new Date();
            String userId0 = ShiroUtils.getUserId();
            for (String userId : userIds) {
                NotifyRecord notifyRecord = new NotifyRecord();
                notifyRecord.setId(sid.nextShort());
                notifyRecord.setUserId(userId);
                notifyRecord.setNotifyId(notifyId);
                notifyRecord.setCreateUser(userId0);
                notifyRecord.setCreateTime(date);
                notifyRecords.add(notifyRecord);
            }
            notifyRecordMapper.batchInsert(notifyRecords);
            //分发消息
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    List<UserLine> users = sessionService.listOnlineUser();
                    for (UserLine user : users) {
                        for (String userId : userIds) {
                            if (userId.equals(user.getId())) {
                                template.convertAndSendToUser(user.toString(), "/queue/notifications", "新消息：" + notify.getTitle());
                            }
                        }
                    }
                }
            });
        }
        return i;
    }

    @Resource
    private NotifyMapper notifyMapper;

    /**
     * 根据用户id查询通知
     *
     * @param userId userId
     * @return map
     */
    @Override
    public List<Map<String, Object>> myNotifyList(String userId) {
        return notifyMapper.myNotifyList(userId);
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        System.out.println(l/(24*3600*1000));
    }
}
